home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Control Common / CCPContainer.cpp < prev    next >
C/C++ Source or Header  |  1996-12-31  |  4KB  |  181 lines

  1. #include "ocheaders.h"
  2. #include <LArray.h>
  3. #include "CConnectionPoint.h"
  4. #include "CEnumConnectionPoints.h"
  5. #include "CCPContainer.h"
  6.  
  7. #define APPENDITEM 32000    // Large number to force appending to an array
  8.  
  9.  
  10. //
  11. //  CCPContainer::CCPContainer
  12. //
  13. CCPContainer::CCPContainer(short NumConnections)
  14. {
  15.     // Allocate memory for the connection points array
  16.     m_ConnectionPoints = new LArray( (sizeof(IConnectionPoint*)) * NumConnections);
  17.     
  18. }
  19.  
  20.  
  21. //
  22. //  CCPContainer::~CCPContainer
  23. //
  24. CCPContainer::~CCPContainer(void)
  25. {
  26.     // if we have connection points, release them
  27.     if ( m_ConnectionPoints )
  28.     {
  29.         short                 i;
  30.         IConnectionPoint*    cp;
  31.         
  32.         // Release each connection point
  33.         for ( i = 1; i <= m_ConnectionPoints->GetCount(); i++)
  34.         {
  35.             m_ConnectionPoints->FetchItemAt(i, &cp);
  36.             cp->Release();
  37.         }
  38.         
  39.         // Free the array
  40.         delete m_ConnectionPoints;
  41.     }
  42. }
  43.  
  44.  
  45. //
  46. //  CCPContainer::AddConnectionPoint
  47. //
  48. STDMETHODIMP
  49. CCPContainer::AddConnectionPoint(IID InterfaceID)
  50. {
  51. #pragma unused (InterfaceID)
  52.     IConnectionPoint*     cp;
  53.     CConnectionPoint*    ConnectionPoint;
  54.     
  55.     // Create a new ConnectionPoint object
  56.     ConnectionPoint = new CConnectionPoint((void*) this, InterfaceID);
  57.     
  58.     // Get the IConnectionPoint interface
  59.     ConnectionPoint->QueryInterface(IID_IConnectionPoint, (void**) &cp);
  60.     
  61.     // Insert the IConnectionPoint* interface into the array
  62.     m_ConnectionPoints->InsertItemsAt(1, APPENDITEM, &cp);
  63.     
  64.     // Add our reference to this connection point
  65.     cp->AddRef();
  66.     
  67.     return ResultFromScode(S_OK);
  68. }
  69.  
  70.  
  71. //
  72. //  CCPContainer::IUnknown::QueryInterface
  73. //
  74. //  Returns a pointer to the specified interface on a component to which a
  75. //  client currently holds an interface pointer.
  76. //
  77. STDMETHODIMP
  78. CCPContainer::QueryInterface(REFIID RefID, void** Obj)
  79. {
  80.     void* pv = nil;
  81.  
  82.     if (RefID == IID_IUnknown )
  83.         pv = (void*) this;
  84.     else if (RefID == IID_IConnectionPointContainer) 
  85.         pv = (void*) (IConnectionPointContainer*) this;
  86.  
  87.     *Obj = pv;
  88.     
  89.     // if we got an interface, ref it and return ok
  90.     if ( pv )
  91.     {
  92.             ((IUnknown*) pv)->AddRef();
  93.         return ResultFromScode(S_OK);
  94.     }
  95.     else
  96.         return ResultFromScode(E_NOINTERFACE);
  97. }
  98.  
  99.  
  100. //
  101. //  CCPContainer::IUnknown::AddRef
  102. //
  103. //  Increments the reference count for the calling interface.
  104. //
  105. STDMETHODIMP_(ULONG)
  106. CCPContainer::AddRef(void)
  107. {
  108.     return ++m_RefCount;
  109. }
  110.  
  111.  
  112. //
  113. //  CCPContainer::IUnknown::Release
  114. //
  115. //  Decrements the reference count for the calling interface on a object.  If
  116. //  the reference count on the object falls to zero, the object is freed.
  117. //
  118. STDMETHODIMP_(ULONG)
  119. CCPContainer::Release(void)
  120. {
  121.     if (--m_RefCount != 0)
  122.         return m_RefCount;
  123.  
  124.     delete this;
  125.     return 0;
  126. }
  127.  
  128.  
  129. //=--------------------------------------------------------------------------=
  130. //  CCPContainer::IConnectionPointContainer::EnumConnectionPoints
  131. //=--------------------------------------------------------------------------=
  132. STDMETHODIMP
  133. CCPContainer::EnumConnectionPoints(IEnumConnectionPoints** EnumCP)
  134. {
  135.     CEnumConnectionPoints*    EnumConnections;
  136.     
  137.     *EnumCP = nil;
  138.     
  139.     // Dont do this if we don't have any connections
  140.     if ( m_ConnectionPoints && m_ConnectionPoints->GetCount() > 0 )
  141.     {
  142.         // Create the enumerator object from our connection points
  143.         EnumConnections = new CEnumConnectionPoints(m_ConnectionPoints);
  144.         
  145.         // Get the enumerator interface and add reference
  146.         if ( EnumConnections )
  147.         {
  148.             EnumConnections->QueryInterface(IID_IEnumConnectionPoints, (void**) EnumCP);
  149.         }
  150.     }
  151.     
  152.     return ResultFromScode(S_OK);
  153. }
  154.  
  155.  
  156. //=--------------------------------------------------------------------------=
  157. //  CCPContainer::IConnectionPointContainer::FindConnectionPoint
  158. //=--------------------------------------------------------------------------=
  159. STDMETHODIMP
  160. CCPContainer::FindConnectionPoint(REFIID RefID, IConnectionPoint** ConnectionPoint)
  161. {
  162.     short                 i;
  163.     IID                    InterfaceID;
  164.     IConnectionPoint*    cp;    
  165.     
  166.     // Look through all our connection points for the one specified by RefID
  167.     for ( i = 1; i <= m_ConnectionPoints->GetCount(); i ++)
  168.     {
  169.         m_ConnectionPoints->FetchItemAt(i, &cp);
  170.         cp->GetConnectionInterface(&InterfaceID);
  171.         
  172.         // if this is the one, query it for the connection point interface
  173.         if ( InterfaceID == RefID )
  174.             return cp->QueryInterface(IID_IConnectionPoint, (void**) ConnectionPoint);
  175.     }
  176.     
  177.     return ResultFromScode(E_NOINTERFACE);
  178. }
  179.  
  180.  
  181.